package org.xqyy.eval.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xqyy.eval.model.*;
import org.xqyy.eval.service.IEvalMasterService;
import org.xqyy.eval.service.IQuestionService;
import org.xqyy.eval.service.ITestPaperService;
import org.xqyy.eval.util.DataFormatUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 评价试卷填报处理
 */
@Controller
@RequestMapping("/evalmaster")
public class EvalMasterController {

    @Autowired
    private IEvalMasterService evalMasterService;
    @Autowired
    private ITestPaperService paperService;
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询之前评价是否是都是满分
     * @param request
     * @param testPaper
     * @return
     */
    @RequestMapping("/evalIsFullscore")
    @ResponseBody
    public  boolean evalIsFullscore(HttpServletRequest request,TestPaper  testPaper){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        String dutyId = user.getDeptId();
        return evalMasterService.evalIsFullscore(dutyId,testPaper.getCycledate());
    }

    /**
     * 提交满意度调查问卷
     * @param request
     * @param evalmasterjson
     * @param testPaper
     * @return
     */
    @RequestMapping("/submitEvalMaster")
    @ResponseBody
    public  boolean submitEvalMaster(HttpServletRequest request, String evalmasterjson, TestPaper  testPaper){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        JSONArray jsonArray = JSON.parseArray(evalmasterjson);
        Iterator iterator =  jsonArray.iterator();
        List<TEvalMaster> evalMasterList = new ArrayList<>();
        while (iterator.hasNext()){
            JSONObject jsonObject = (JSONObject)iterator.next();
            TEvalMaster evalMaster = new TEvalMaster();
            evalMaster.setEvalyearmonth(testPaper.getCycledate());
            evalMaster.setEvaldutyId(jsonObject.getString("id"));
            evalMaster.setEvaldatetime(new Date());
            evalMaster.setDutyId(user.getDeptId());
            evalMaster.setPaperid(testPaper.getPaperId());
            if (!"如不满意需填写说明".equals(jsonObject.getString("context"))){
                evalMaster.setInfo(jsonObject.getString("context"));
            }
            List<TEvalDetail> evalDetails = new ArrayList<>();
            List<TQuestion> questionList = questionService.selectQuestionByPaperId(testPaper.getPaperId());
            for (TQuestion tQuestion : questionList) {
                String key = "question"+tQuestion.getId();
                Integer fraction = jsonObject.getInteger(key);
                TEvalDetail evalDetail = new TEvalDetail();
                evalDetail.setAnswer(Float.parseFloat(fraction.toString()));
                evalDetail.setQuestionid(tQuestion.getId());
                evalDetails.add(evalDetail);
            }
            evalMaster.setEvalDetails(evalDetails);
            evalMasterList.add(evalMaster);
        }
        return  evalMasterService.submitEvalMaster(evalMasterList);
    }

    @RequestMapping("/evalEdMasterPage")
    public ModelAndView evalEdMasterPage(){
        Date date = Calendar.getInstance().getTime();
        String cycledate =  DataFormatUtil.defectDateFormat.format(date);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cycledate",cycledate);
        modelAndView.setViewName("jsp/eval/evalEdMasterPage");
        return modelAndView;
    }

    @RequestMapping("/evalEdMaster")
    @ResponseBody
    public List<TEvalMaster> evalEdMaster(HttpServletRequest request,String cycledate){
        HttpSession session = request.getSession();
        TUser user = (TUser)  session.getAttribute("user");
        List<TEvalMaster> evalMasterList = evalMasterService
                .selectEvalEdMasterByYearMonthAndDutyId(user.getDeptId(),cycledate);
        return  evalMasterList;
    }
}
